
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/numeric-computation.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:32 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Numeric Computation &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="numeric-computation.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="ODE" href="solvers/ode.html" />
    <link rel="prev" title="Number Theory" href="ntheory.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="solvers/ode.html" title="ODE"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ntheory.html" title="Number Theory"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Numeric Computation</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="numeric-computation">
<span id="id1"></span><h1>Numeric Computation<a class="headerlink" href="#numeric-computation" title="Permalink to this headline">¶</a></h1>
<p>Symbolic computer algebra systems like SymPy facilitate the construction and
manipulation of mathematical expressions.  Unfortunately when it comes time
to evaluate these expressions on numerical data, symbolic systems often have
poor performance.</p>
<p>Fortunately SymPy offers a number of easy-to-use hooks into other numeric
systems, allowing you to create mathematical expressions in SymPy and then
ship them off to the numeric system of your choice.  This page documents many
of the options available including the <code class="docutils literal notranslate"><span class="pre">math</span></code> library, the popular array
computing package <code class="docutils literal notranslate"><span class="pre">numpy</span></code>, code generation in <code class="docutils literal notranslate"><span class="pre">Fortran</span></code> or <code class="docutils literal notranslate"><span class="pre">C</span></code>, and the
use of the array compiler <code class="docutils literal notranslate"><span class="pre">Aesara</span></code>.</p>
<section id="subs-evalf">
<h2>Subs/evalf<a class="headerlink" href="#subs-evalf" title="Permalink to this headline">¶</a></h2>
<p>Subs is the slowest but simplest option.  It runs at SymPy speeds.
The <code class="docutils literal notranslate"><span class="pre">.subs(...).evalf()</span></code> method can substitute a numeric value
for a symbolic one and then evaluate the result within SymPy.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="n">subs</span><span class="o">=</span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="mf">3.14</span><span class="p">})</span>
<span class="go">0.000507214304613640</span>
</pre></div>
</div>
<p>This method is slow.  You should use this method production only if performance
is not an issue.  You can expect <code class="docutils literal notranslate"><span class="pre">.subs</span></code> to take tens of microseconds. It
can be useful while prototyping or if you just want to see a value once.</p>
</section>
<section id="lambdify">
<h2>Lambdify<a class="headerlink" href="#lambdify" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">lambdify</span></code> function translates SymPy expressions into Python functions,
leveraging a variety of numerical libraries.  It is used as follows:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mf">3.14</span><span class="p">)</span>
<span class="go">0.000507214304614</span>
</pre></div>
</div>
<p>Here lambdify makes a function that computes <code class="docutils literal notranslate"><span class="pre">f(x)</span> <span class="pre">=</span> <span class="pre">sin(x)/x</span></code>.  By default
lambdify relies on implementations in the <code class="docutils literal notranslate"><span class="pre">math</span></code> standard library. This
numerical evaluation takes on the order of hundreds of nanoseconds, roughly two
orders of magnitude faster than the <code class="docutils literal notranslate"><span class="pre">.subs</span></code> method.  This is the speed
difference between SymPy and raw Python.</p>
<p>Lambdify can leverage a variety of numerical backends.  By default it uses the
<code class="docutils literal notranslate"><span class="pre">math</span></code> library.  However it also supports <code class="docutils literal notranslate"><span class="pre">mpmath</span></code> and most notably,
<code class="docutils literal notranslate"><span class="pre">numpy</span></code>.  Using the <code class="docutils literal notranslate"><span class="pre">numpy</span></code> library gives the generated function access to
powerful vectorized ufuncs that are backed by compiled C code.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="s2">&quot;numpy&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">[ 0.84147098  0.84119981  0.84092844 ... -0.05426074 -0.05433146</span>
<span class="go"> -0.05440211]</span>
</pre></div>
</div>
<p>If you have array-based data this can confer a considerable speedup, on the
order of 10 nano-seconds per element. Unfortunately numpy incurs some start-up
time and introduces an overhead of a few microseconds.</p>
<p>CuPy is a NumPy-compatible array library that mainly runs on CUDA, but has
increasing support for other GPU manufacturers. It can in many cases be used as
a drop-in replacement for numpy.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="s2">&quot;cupy&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">cupy</span> <span class="k">as</span> <span class="nn">cp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">cp</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="c1"># perform the computation</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cp</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="c1"># explicitly copy from GPU to CPU / numpy array</span>
<span class="go">[ 0.84147098  0.84119981  0.84092844 ... -0.05426074 -0.05433146</span>
<span class="go"> -0.05440211]</span>
</pre></div>
</div>
</section>
<section id="ufuncify">
<h2>uFuncify<a class="headerlink" href="#ufuncify" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">autowrap</span></code> module contains methods that help in efficient computation.</p>
<ul class="simple">
<li><p><a class="reference internal" href="codegen.html#id1"><span class="std std-ref">autowrap</span></a> method for compiling code generated by the
<a class="reference internal" href="codegen.html#codegen-prose"><span class="std std-ref">codegen</span></a> module, and wrap the binary for use in python.</p></li>
<li><p><a class="reference internal" href="codegen.html#binary-function"><span class="std std-ref">binary_function</span></a> method automates the steps needed to autowrap
the SymPy expression and attaching it to a <code class="docutils literal notranslate"><span class="pre">Function</span></code> object with <code class="docutils literal notranslate"><span class="pre">implemented_function()</span></code>.</p></li>
<li><p><a class="reference internal" href="codegen.html#ufuncify-method"><span class="std std-ref">ufuncify</span></a> generates a binary function that supports broadcasting
on numpy arrays using different backends that are faster as compared to <code class="docutils literal notranslate"><span class="pre">subs/evalf</span></code>
and <code class="docutils literal notranslate"><span class="pre">lambdify</span></code>.</p></li>
</ul>
<p>The API reference of all the above is listed here: <a class="reference internal" href="utilities/autowrap.html#module-sympy.utilities.autowrap" title="sympy.utilities.autowrap"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympy.utilities.autowrap()</span></code></a>.</p>
</section>
<section id="aesara">
<h2>Aesara<a class="headerlink" href="#aesara" title="Permalink to this headline">¶</a></h2>
<p>SymPy has a strong connection with
<a class="reference external" href="https://aesara.readthedocs.io/en/latest/">Aesara</a>, a mathematical array
compiler.  SymPy expressions can be easily translated to Aesara graphs and then
compiled using the Aesara compiler chain.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.printing.aesaracode</span> <span class="kn">import</span> <span class="n">aesara_function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">aesara_function</span><span class="p">([</span><span class="n">x</span><span class="p">],</span> <span class="p">[</span><span class="n">expr</span><span class="p">])</span>
</pre></div>
</div>
<p>If array broadcasting or types are desired then Aesara requires this extra
information</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">aesara_function</span><span class="p">([</span><span class="n">x</span><span class="p">],</span> <span class="p">[</span><span class="n">expr</span><span class="p">],</span> <span class="n">dims</span><span class="o">=</span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">dtypes</span><span class="o">=</span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="s1">&#39;float64&#39;</span><span class="p">})</span>
</pre></div>
</div>
<p>Aesara has a more sophisticated code generation system than SymPy’s C/Fortran
code printers.  Among other things it handles common sub-expressions and
compilation onto the GPU.  Aesara also supports SymPy Matrix and Matrix
Expression objects.</p>
</section>
<section id="so-which-should-i-use">
<h2>So Which Should I Use?<a class="headerlink" href="#so-which-should-i-use" title="Permalink to this headline">¶</a></h2>
<p>The options here were listed in order from slowest and least dependencies to
fastest and most dependencies.  For example, if you have Aesara installed then
that will often be the best choice.  If you don’t have Aesara but do have
<code class="docutils literal notranslate"><span class="pre">f2py</span></code> then you should use <code class="docutils literal notranslate"><span class="pre">ufuncify</span></code>. If you have been comfortable using
lambdify with the numpy module, but have a GPU, CuPy can provide substantial
speedups with little effort.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 25%" />
<col style="width: 10%" />
<col style="width: 43%" />
<col style="width: 22%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Tool</p></th>
<th class="head"><p>Speed</p></th>
<th class="head"><p>Qualities</p></th>
<th class="head"><p>Dependencies</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>subs/evalf</p></td>
<td><p>50us</p></td>
<td><p>Simple</p></td>
<td><p>None</p></td>
</tr>
<tr class="row-odd"><td><p>lambdify</p></td>
<td><p>1us</p></td>
<td><p>Scalar functions</p></td>
<td><p>math</p></td>
</tr>
<tr class="row-even"><td><p>lambdify-numpy</p></td>
<td><p>10ns</p></td>
<td><p>Vector functions</p></td>
<td><p>numpy</p></td>
</tr>
<tr class="row-odd"><td><p>ufuncify</p></td>
<td><p>10ns</p></td>
<td><p>Complex vector expressions</p></td>
<td><p>f2py, Cython</p></td>
</tr>
<tr class="row-even"><td><p>lambdify-cupy</p></td>
<td><p>10ns</p></td>
<td><p>Vector functions on GPUs</p></td>
<td><p>cupy</p></td>
</tr>
<tr class="row-odd"><td><p>Aesara</p></td>
<td><p>10ns</p></td>
<td><p>Many outputs, CSE, GPUs</p></td>
<td><p>Aesara</p></td>
</tr>
</tbody>
</table>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Numeric Computation</a><ul>
<li><a class="reference internal" href="#subs-evalf">Subs/evalf</a></li>
<li><a class="reference internal" href="#lambdify">Lambdify</a></li>
<li><a class="reference internal" href="#ufuncify">uFuncify</a></li>
<li><a class="reference internal" href="#aesara">Aesara</a></li>
<li><a class="reference internal" href="#so-which-should-i-use">So Which Should I Use?</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="ntheory.html"
                        title="previous chapter">Number Theory</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="solvers/ode.html"
                        title="next chapter">ODE</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/modules/numeric-computation.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="solvers/ode.html" title="ODE"
             >next</a> |</li>
        <li class="right" >
          <a href="ntheory.html" title="Number Theory"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Numeric Computation</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/numeric-computation.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:32 GMT -->
</html>